#include "TcpProtocolTask.h"

void Call::TcpProtocalTask::Run()
{
    while(1)
    {
        if (m_task_ref != 0)
        {
            ClearDisconnected();
            std::this_thread::sleep_for(std::chrono::seconds(m_NumSecondsInterval));
        }
    }
}

void Call::TcpProtocalTask::ClearDisconnected()
{
    int lastNum = m_task_ref->CountLinkNum();
    m_task_ref->LockTaskList();

    LOGI("clear disconnected");
    std::vector<Call::TcpClientTask*> tasks_target;

    for (auto& task : m_task_ref->m_tasks)
    {
        if(task)
        {

            if (task->m_State == TcpClientTask::State::Running)
            {
                tasks_target.push_back(task);
            }
            else
            {
                LOGI("delete disconnected socket %s %d", task->m_tcpSocket.IP(), task->m_tcpSocket.Port());
                delete task;
            }
        }
    }
    
    m_task_ref->m_tasks = tasks_target;
    int currentNum = m_task_ref->CountLinkNum();

    LOGI("last is %d and current is %d", lastNum, currentNum);


}
